/**
 * 
 */
package es.aradesh.ministryassist.logic.ministry;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.provider.BaseColumns;

import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.db.StreetColumns;

/**
 * @author xmorales
 * 
 */
public class StreetBuilder {

	public static long createStreet(DbAdapter db, Territory t, String name,
			int type) {
		long id = db.insertStreet(t.getId(), name, type);
		return id;
	}

	public static Street getStreetById(DbAdapter db, long id) {
		Street result = null;
		ContentValues val = db.selectById(DbAdapter.TABLE_STREET, id);
		result = new Street(id, TerritoryBuilder.getTerritoryById(db, val
				.getAsLong(StreetColumns.TERRITORY_ID).longValue()), val
				.getAsString(StreetColumns.NAME), val.getAsInteger(
						StreetColumns.TYPE).intValue());
		return result;
	}

	public static List<Street> getStreetsByTerritory(DbAdapter db,
			Territory parent) {

		List<Street> result = new ArrayList<Street>();
		Street temp = null;
		Cursor c = db.selectTable(DbAdapter.TABLE_STREET,
				StreetColumns.TERRITORY_ID + " = " + parent.getId(), null);
		c.moveToFirst();
		while (!c.isAfterLast()) {
			temp = new Street(c.getLong(c.getColumnIndex(BaseColumns._ID)),
					parent, c.getString(c
							.getColumnIndex(StreetColumns.NAME)),
					c.getInt(c.getColumnIndex(StreetColumns.TYPE)));
			result.add(temp);
			c.moveToNext();
		}
		return result;
	}
	public static Street getStreetByName(DbAdapter db, String name, String town) {
		Street result = null;
		return result;
	}
}
